//判断牌型
const card_type = function(cardlist) {
	if (!cardlist.length) return false;
	let type = "";
	switch (cardlist.length) {
		case 1:
			type = "Single";
			break;
		case 2:
			if (!(type = isKingBoom(cardlist))) {
				type = isDouble(cardlist);
			}
			break;
		case 3:
			type = isThree(cardlist);
			break;
		case 4:
			if (!(type = isBoom(cardlist))) {
				type = isThreeAddOne(cardlist);
			}
			break;
		case 5:
			if (!(type = isSunZi(cardlist))) {
				if (!(type = isThreeAddTwo(cardlist))) {
					type = isBoomAddOne(cardlist);
				}
			}
			break;
		default:
			if (!(type = isSunZi(cardlist))) {
				if (!(type = isLianDui(cardlist))) {
					if (!(type = isFlay(cardlist))) {
						if (!(type = isBoomAddTwo(cardlist))) {
							if (!(type = isFlayAddOne(cardlist))) {
								if (!(type = isFlayAddTwo(cardlist))) {
									type = '';
								}
							}
						}
					}
				}
			}
			break;
	}
	return type;
}
//比较牌型大小
const card_value = function(type, cardlist1, cardlist2) {
	if (
		type == 'Single' || 
		type == 'Double' || 
		type == 'Three' || 
		type == 'Boom' ||
		type == 'ThreeAddOne' ||
		type == 'ThreeAddTwo' ||
		type == 'BoomAddOne' ||
		type == 'BoomAddTwo'
	) {
		if (cardlist1[0] < cardlist2[0]) return false;
	}
	if (
		type == 'Shunzi' || 
		type == 'LianDui' || 
		type == 'Flay' ||
		type == 'FlayAddOne' ||
		type == 'FlayAddTwo'
	) {
		if (cardlist1.length == cardlist2.length) {
			if (cardlist1[0] < cardlist2[0]) return false;
		}
	}

	if (type == 'KingBoom') {
		return false;
	}

	return true;
}

/* 判断牌型 */
//判断对子
function isDouble(cardlist) {
	if (cardlist[0] == cardlist[1]) {
		return 'Double'
	}
	return ''
}
//判断三张
function isThree(cardlist) {
	if (cardlist[0] != cardlist[1]) return '';
	if (cardlist[1] != cardlist[2]) return '';
	if (cardlist[0] != cardlist[2]) return '';
	return 'Three';
}
//判断三带一
function isThreeAddOne(cardlist) {
	if (cardlist[2] != cardlist[3]) {
		if (cardlist[0] != cardlist[1]) return '';
		if (cardlist[1] != cardlist[2]) return '';
		if (cardlist[0] != cardlist[2]) return '';
		return 'ThreeAddOne';
	} else {
		return '';
	}
}

//判断三带二
function isThreeAddTwo(cardlist) {
	if (cardlist[3] == cardlist[4]) {
		if (cardlist[0] != cardlist[1]) return '';
		if (cardlist[1] != cardlist[2]) return '';
		if (cardlist[0] != cardlist[2]) return '';
		return 'ThreeAddTwo';
	} else {
		return '';
	}
}

//判断顺子
function isSunZi(cardlist) {
	if (cardlist.length >= 5 && cardlist.length <= 12) {
		for (let i = 0; i < cardlist.length - 1; i++) {
			if (cardlist[i + 1] != cardlist[i] - 1) {
				return '';
			}
			//顺子不能是2和2王
			if (cardlist[i] == 15 || cardlist[i] == 16 || cardlist[i] == 17) {
				return '';
			}
		}
		return 'ShunZi';
	} else {
		return '';
	}
}

//判断联对
function isLianDui(cardlist) {
	if (cardlist.length >= 6 && cardlist.length % 2 == 0) {
		for (let i = 0; i < cardlist.length; i = i + 2) {
			//连对不能包含2和俩王
			if (cardlist[i] == 15 || cardlist[i] == 16 || cardlist[i] == 17) return '';
			//对子之间是否相等
			if (cardlist[i] != cardlist[i + 1]) return '';
			//2个对子之间是否相差1
			if (cardlist[i + 2])
				if (cardlist[i + 2] != cardlist[i] - 1) return '';
		}
		return 'LianDui';
	} else {
		return '';
	}
}

//判断飞机
function isFlay(cardlist) {

	if (cardlist.length >= 6 && cardlist.length % 3 == 0) {
		for (let i = 0; i < cardlist.length; i = i + 3) {
			//不能包含2和俩王
			if (cardlist[i] == 15 || cardlist[i] == 16 || cardlist[i] == 17) return '';
			if (cardlist[i] != cardlist[i + 1]) return '';
			if (cardlist[i + 1] != cardlist[i + 2]) return '';
			if (cardlist[i] != cardlist[i + 2]) return '';
			if (cardlist[i + 3]) {
				if (cardlist[i] - 1 != cardlist[i + 3]) return '';
			}
		}
		return 'Flay';
	} else {
		return '';
	}
}

//判断飞机带单
function isFlayAddOne(cardlist) {
	if (cardlist.length >= 8 && cardlist.length % 4 == 0) {
		for (let i = 0; i < cardlist.length - (cardlist.length / 4); i = i + 3) {
			//不能包含2和俩王
			if (cardlist[i] == 15 || cardlist[i] == 16 || cardlist[i] == 17) return '';
			if (cardlist[i] != cardlist[i + 1]) return '';
			if (cardlist[i + 1] != cardlist[i + 2]) return '';
			if (cardlist[i] != cardlist[i + 2]) return '';
			if (cardlist[i + 3]) {
				if (cardlist[i] - 1 != cardlist[i + 3]) return '';
			}
		}
		//找出单牌
		let singlearr = cardlist.slice(cardlist.length - (cardlist.length / 4) - 1, cardlist.length - 1);
		if (singlearr.length == 2) {
			if (singlearr[0] != singlearr[1]) {
				return 'FlayAddOne'
			} else {
				return ''
			}
		} else if (singlearr.length == 3) {
			if (singlearr[0] != singlearr[1]) return 'FlayAddOne'
			if (singlearr[1] != singlearr[2]) return 'FlayAddOne'
			if (singlearr[0] != singlearr[2]) return 'FlayAddOne'
			return ''
		} else {
			return 'FlayAddOne'
		}

	}

}
//判断飞机带对
function isFlayAddTwo(cardlist) {
	if (cardlist.length >= 10 && cardlist.length % 5 == 0) {
		for (let i = 0; i < cardlist.length - (cardlist.length / 5) * 2; i = i + 3) {
			//不能包含2和俩王
			if (cardlist[i] == 15 || cardlist[i] == 16 || cardlist[i] == 17) return '';
			if (cardlist[i] != cardlist[i + 1]) return '';
			if (cardlist[i + 1] != cardlist[i + 2]) return '';
			if (cardlist[i] != cardlist[i + 2]) return '';
			if (cardlist[i + 3]) {
				if (cardlist[i] - 1 != cardlist[i + 3]) return '';
			}
		}
		//找出对牌
		let singlearr = cardlist.slice(cardlist.length - (cardlist.length / 5) * 2 - 1, cardlist.length - 1);
		if (singlearr.length == 2) {
			if (singlearr[0] == singlearr[1]) {
				return 'FlayAddTwo'
			} else {
				return ''
			}
		} else {
			for (let i = 0; i < singlearr.length; i = i + 2) {
				//对子之间是否相等
				if (cardlist[i] != cardlist[i + 1]) return '';
			}
			return 'FlayAddTwo';
		}
	}

}
//判断王炸
function isKingBoom(cardlist) {
	if (cardlist[0] == 16 && cardlist[1] == 17) {
		return 'KingBoom';
	} else {
		return '';
	}
}

//判断炸弹
function isBoom(cardlist) {
	for (let i = 0; i < cardlist.length; i++) {
		if (i == cardlist.length) {
			if (cardlist[0] != cardlist[i])
				return '';
		} else {
			if (cardlist[i + 1])
				if (cardlist[i] != cardlist[i + 1]) return '';
		}
	}
	return 'Boom';
}

//判断炸弹带单张
function isBoomAddOne(cardlist) {
	if (cardlist[3] != cardlist[4]) {
		if (cardlist[0] == cardlist[1] && cardlist[1] == cardlist[2] && cardlist[2] == cardlist[3]) {
			return 'BoomAddOne';
		} else {
			return ''
		}
	} else {
		return '';
	}
}

//判断炸弹带对子
function isBoomAddTwo(cardlist) {
	//后2张是对子
	if (cardlist[4] == cardlist[5]) {
		if (cardlist[0] == cardlist[1] && cardlist[1] == cardlist[2] & cardlist[2] == cardlist[3]) {
			return 'BoomAddTwo';
		}
	} else {
		return '';
	}
}




export {
	card_type,
	card_value
}
